<?php


namespace App\Http\Controllers;

use App\Helpers\CodeResponse;
use App\Helpers\XLog;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller as BaseController;

class ApiBaseController extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $auth = 'auth:user';
    protected ?array $only = null;
    protected ?array $except = null;

    public function __construct()
    {
        $option = ['index'];
        if (!is_null($this->only)) {
            $option['only'] = $this->only;
        }
        if (!is_null($this->except)) {
            $option['except'] = $this->except;
        }
        $this->middleware($this->auth, $option);
    }


    protected function codeReturn($errno, $errmsg, $data = null, $http_status = 200, array $headers = [], $options = 0)
    {
        $ret = [
            'code' => $errno,
            'msg' => $errmsg
        ];
        if ($data instanceof JsonResource) {
            return $data->additional($ret);
        }
        $ret['data'] = $data;
        return response()->json($ret, $http_status, $headers, $options);
    }

    protected function noContent($message = 'No content')
    {
        return $this->codeReturn(null, $message, null, Response::HTTP_NO_CONTENT);
    }

    protected function success($data = [], $message = 'success')
    {
        return $this->codeReturn(0, $message, $data);
    }


    // 附带其他参数
    protected function successWith($data = [], $extension = [], $message = 'success', $http_status = 200, array $headers = [], $options = 0)
    {
        $ret = [
            'code' => 0,
            'message' => $message
        ];
        $ret = array_merge($ret, $extension);
        if ($data instanceof JsonResource) {
            return $data->additional($ret);
        }
        $ret['data'] = $data;
        return response()->json($ret, $http_status, $headers, $options);
    }

    protected function create($data = null, $message = 'create', string $location = '')
    {
        $headers = [];
        if ($location) {
            $headers = ['Location' => $location];
        }
        return $this->codeReturn(0, $message, $data, Response::HTTP_CREATED, $headers);
    }

    protected function fail($errno, $errmsg)
    {
        return $this->codeReturn($errno, $errmsg);
    }

    protected function notAllDone($data = null, $message = '未全部完成', string $location = '')
    {
        $headers = [];
        if ($location) {
            $headers = ['Location' => $location];
        }
        [$code, $msg] = CodeResponse::NOT_ALL_DONE;
        return $this->codeReturn($code, $message, $data, Response::HTTP_PARTIAL_CONTENT, $headers);
    }

    protected function resetForm($data = null, $message = '重置表单')
    {
        [$code, $msg] = CodeResponse::RESET_THE_FORM;
        return $this->codeReturn($code, $message, $data, Response::HTTP_RESET_CONTENT);
    }

    protected function redirect($data = null, $message = 'redirect', string $location = '')
    {
        $headers = [];
        if ($location) {
            $headers = ['Location' => $location];
        }
        [$code, $msg] = CodeResponse::REDIRECT;
        return $this->codeReturn($code, $message, $data, 200, $headers);
    }
}
